package com.jh.user.business.impl;

import cn.jh.common.utils.RandomUtils;
import com.jh.user.business.BrandManageBusiness;
import com.jh.user.business.UserAutoUpgradeBusiness;
import com.jh.user.pojo.*;
import com.jh.user.pojo.enums.PayType;
import com.jh.user.repository.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserAutoUpgradeBusinessImpl implements UserAutoUpgradeBusiness {

	private static final Logger LOG = LoggerFactory.getLogger(UserAutoUpgradeBusinessImpl.class);

	@Autowired
	private BrandAutoUpgradeRepository brandAutoUpgradeRepository;

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private ChannelRateRepository channelRateRepository;

	@Autowired
	private ChannelRepository channelRepository;

	@Autowired
	private BrandRepository brandRepository;

	@Autowired
	private BrandAutoUpgradeConfigRepository brandAutoUpgradeConfigRepository;

	@Autowired
	private ThirdLevelRateRepository thirdLevelRateRepository;

	@Autowired
	private ThirdLevelDistributionRepository thirdLevelDistriRepository;

	@Autowired
	private BrandManageBusiness brandMangeBusiness;

	@Autowired
	private EntityManager em;

	@Transactional
	public List<User> getNeedAutoGradeUser() {

		/*** 首先判断通道 */
		List<BrandAutoUpdateConfig> autoUpgrades = brandAutoUpgradeConfigRepository.findBrandAutoUpgradeConfig();

		for (BrandAutoUpdateConfig config : autoUpgrades) {

			List<User> users = new ArrayList<User>();
			users = userRepository.findNewAutoUpgradeUser(config.getGrade() - 1, config.getGrade(),
					config.getPeopleNum(), config.getBrandId());

			if (users != null && users.size() != 0) {
				LOG.info("可以升级的人数................." + users.size());
				for (User user : users) {
					Brand brand = brandRepository.findBrandByid(user.getBrandId());
					LOG.info("当前升级。。。。。。。。。。。。。。。。。。。。。" + user.getId());
					if (brand.getManageid() == user.getId()) {
						continue;
					}
					/*** 先清除 */
					List<Channel> channelLists = channelRepository.findAll();
					for (Channel channel : channelLists) {
						channelRateRepository.delChannelRateByUserid(user.getId(), user.getBrandId(), channel.getId());
					}

					ThirdLevelDistribution distribution = thirdLevelDistriRepository.findAllThirdLevelByBrandidandlevel(
							user.getBrandId(), new Long(config.getGrade()).intValue());
					if (distribution == null) {
						continue;
					}
					List<ThirdLevelRate> thirdRates = thirdLevelRateRepository
							.findAllThirdLevelRatesBylevelid(distribution.getId());

					if (thirdRates != null) {
						LOG.info("可以升级的通道................." + thirdRates.size());
					}

					List<ChannelRate> channels = new ArrayList<ChannelRate>();
					if (thirdRates != null && thirdRates.size() > 0) {

						for (ThirdLevelRate auto : thirdRates) {
							BrandRate brandRate = brandMangeBusiness.findRateByBrandAndChannel(user.getBrandId(),
									auto.getChannelId(),user.getGrade(), PayType.TRADE.getValue());
							if (brandRate == null) {
								LOG.info("贴牌没配置费率的通道是..brandId..." + user.getBrandId() + "..........ChannelId."
										+ auto.getChannelId());
								continue;
							}
							ChannelRate channelrate = new ChannelRate();
							channelrate.setId(Long.parseLong(RandomUtils.generateNumString(6)));
							channelrate.setBrandId(user.getBrandId());
							channelrate.setChannelId(auto.getChannelId());
							channelrate.setCreateTime(new Date());
							channelrate.setExtraFee(brandRate.getExtraFee());
							channelrate.setRate(auto.getRate());
							channelrate.setUserId(user.getId());
							channelrate.setGradeRate(brandRate.getGradeRate());
							channels.add(channelrate);
						}

						user.setGrade(config.getGrade() + "");

					}

					LOG.info("更新前。。。。。。。。。。。。。。。。。。。。。");
					LOG.info("channelsratesize ========" + channels.size());
					LOG.info("user gradeid ========" + user.getGrade());
					channelRateRepository.save(channels);

//					UserNew model = (UserNew)user;
//					if("6".equals(brand.getBrandType())){
//						userRepository.save(model);
//					}else{
//						userRepository.save(model);
//						userRepositoryOld.save(user);
//					}
					em.flush();
					LOG.info("更新后 。。。。。。。。。。。。。。。。。。。。。");
					LOG.info("升级成功.................userid=" + user.getId());

				}
			} else {
				LOG.info("可以升级的人数................." + 0);
			}

		}
		return null;
	}

	@Override
	public BrandAutoUpgrade findBrandAutoUpgradeBybrandidAndchannelId(long brandid, long channelId) {
		BrandAutoUpgrade brandAutoUpgrade = new BrandAutoUpgrade();
		brandAutoUpgrade = brandAutoUpgradeRepository.findBrandAutoUpgradeBybrandidAndchannelId(brandid, channelId);
		return brandAutoUpgrade;
	}

	@Transactional
	@Override
	public BrandAutoUpgrade saveBrandAutoUpgrade(BrandAutoUpgrade brandAutoUpgrade) {

		return brandAutoUpgradeRepository.save(brandAutoUpgrade);
	}

	@Override
	public List<BrandAutoUpgrade> findBrandAutoUpgradeByBrandid(long brandid) {

		return brandAutoUpgradeRepository.findBrandAutoUpgradeBybrandid(brandid);
	}

	@Transactional
	@Override
	public BrandAutoUpdateConfig saveBrandAutoUpdateConfig(BrandAutoUpdateConfig brandAutoUpdateConfig) {

		BrandAutoUpdateConfig result = brandAutoUpgradeConfigRepository.save(brandAutoUpdateConfig);
		em.flush();
		return result;
	}

	@Override
	public BrandAutoUpdateConfig getBrandAutoByBrandidAndGrade(long brandid, long grade) {

		return brandAutoUpgradeConfigRepository.findBrandAutoUpgradeConfigBybrandidAndgrade(brandid, grade);
	}

	@Override
	public BrandAutoUpdateConfig getBrandAutoByBrandidAndGradeNostutas(long brandid, long grade) {

		return brandAutoUpgradeConfigRepository.findBrandAutoUpgradeConfigBybrandidAndgradeNostatus(brandid, grade);
	}

	@Override
	public List<BrandAutoUpdateConfig> getBrandAutoConfigByBrandidNostutas(long brandid) {

		return brandAutoUpgradeConfigRepository.findBrandAutoUpgradeConfigBybrandidNostutas(brandid);
	}

	@Override
	public List<BrandAutoUpdateConfig> getBrandAutoConfigByBrandid(long brandid) {

		return brandAutoUpgradeConfigRepository.findBrandAutoUpgradeConfigBybrandid(brandid);
	}

}
